package com.study.forumsystemproject.service.impl;

import com.study.forumsystemproject.common.MessageState;
import com.study.forumsystemproject.common.Result;
import com.study.forumsystemproject.common.ResultCode;
import com.study.forumsystemproject.dao.MessageMapper;
import com.study.forumsystemproject.exception.ApplicationException;
import com.study.forumsystemproject.model.Message;
import com.study.forumsystemproject.model.User;
import com.study.forumsystemproject.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * program: forum-system-project
 * <p>
 * description: 信息接口的实现
 * <p>
 * author: xiaoxie
 * <p>
 * create: 2024-08-19 22:52
 **/
@Service
@Slf4j
public class MessageImpl implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserImpl userImpl;
    @Override
    public void sendMessage(Message message) {
        if (message == null || message.getContent() == null
            || message.getPostUserId() == null || message.getReceiveUserId() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //判断接收者是否存在
        User userInfo = userImpl.getUserInfo(message.getReceiveUserId());
        if (userInfo == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //设置默认值
        //设置为未读状态
        message.setState(0);
        message.setDeleteFlag(0);
        //插入数据
        int row = messageMapper.insertSelective(message);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.fail(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Integer selectNotRedByReceiveUserId(Long receiveUserId) {
        if (receiveUserId == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //判断用户是否存在
        User userInfo = userImpl.getUserInfo(receiveUserId);
        if (userInfo == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        return messageMapper.selectNotRedByReceiveUserId(receiveUserId);
    }

    @Override
    public Message selectById(Long id) {
        if (id == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        return messageMapper.selectByPrimaryKey(id);
    }


    @Override
    public void reply(Message message) {
        if(message == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //根据id查询数据库中是否有message对象
        Message extMassage = selectById(message.getId());
        if (extMassage == null || extMassage.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        if(extMassage.getPostUserId() == null || extMassage.getReceiveUserId() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //更新状态
        Message updateMessage = new Message();
        updateMessage.setId(message.getId());
        updateMessage.setState(MessageState.REPLIED.getCode());
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.fail(ResultCode.ERROR_SERVICES));
        }
        //插入数据
        Message insertMessage = new Message();
        insertMessage.setPostUserId(extMassage.getReceiveUserId());
        insertMessage.setReceiveUserId(extMassage.getPostUserId());
        insertMessage.setState(MessageState.UNREAD.getCode());
        insertMessage.setContent(message.getContent());
        insertMessage.setDeleteFlag(0);
        int row1 = messageMapper.insertSelective(insertMessage);
        if (row1 != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.fail(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public List<Message> selectRedByReceiveUserId(Long receiveUserId) {
        //校验参数
        if (receiveUserId == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //判断用户是否存在
        User userInfo = userImpl.getUserInfo(receiveUserId);
        if (userInfo == null || userInfo.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //判断用户状态
        if(userInfo.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_USER_BANNED));
        }
        return messageMapper.getAllMessageByReceiveUserId(receiveUserId);
    }

    @Override
    public void updateRedById(Long id, MessageState state) {
        //校验参数
        if(id == null || state == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //根据id获取私信信息
        Message message = selectById(id);
        if(message == null || message.getDeleteFlag() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //确定一下发送者和接收者的id不一样
        if(message.getReceiveUserId() == message.getPostUserId()) {
            log.warn("接收者和发送者id禁止相同");
            throw new ApplicationException("接收者和发送者id禁止相同");
        }
        //查询一下是否存在对象
        User userInfo = userImpl.getUserInfo(message.getReceiveUserId());
        if (userInfo == null || userInfo.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        if(userInfo.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_USER_BANNED));
        }
        if(message.getState() == 0) {
            Message updateMessage = new Message();
            updateMessage.setId(id);
            updateMessage.setState(state.getCode());
            int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
            if(row != 1) {
                log.warn(ResultCode.ERROR_SERVICES.toString());
                throw new ApplicationException(Result.fail(ResultCode.ERROR_SERVICES));
            }
        }
    }

}
